﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using System.Text;

namespace mmoss.orm 
{
    public partial class Patient
    {

        public PatientError Error
        {
            get
            {
                //no red card date
                if (!this.patientDatePrescriptionExpiration.HasValue)
                {
                    return PatientError.NO_REDCARD;
                }
                //red card expired
                if (this.patientDatePrescriptionExpiration < DateTime.Now)
                {
                    return PatientError.EXPIRED_REDCARD;
                }
                //todo - add sale limits and banned patient info
                return PatientError.NONE;   
            }
        }
    }
    public partial class PatientCheckIn
    {
        public string patientLastName
        {
            get { return Patient.patientLastName; }
        }
        public string patientFirstName
        {
            get { return Patient.patientFirstName;}
        }
        public int waitTime
        {
            get { return (DateTime.Now - checkInDate).Minutes; }
        }
    }
    public static class ExtensionPatient
    {
        public static List<PatientCheckIn> GetCheckOutPre(this Table<PatientCheckIn> table, int l_locationID)
        {
            return (from objs in table
                    where objs.locationID == l_locationID
                    && objs.checkOutPre!=null
                    && objs.checkOutPost == null
                    select objs
                    ).ToList();
        }
        public static List<PatientCheckIn> GetCheckin(this Table<PatientCheckIn> table, int l_locationID)
        {
            return (from objs in table
                    where objs.locationID == l_locationID
                    && objs.checkOutPre == null
                    && objs.checkOutPost == null
                    select objs
                    ).ToList();
        }
        public static Patient Get(this Table<Patient> table
            , string driversLicenseNum)
        {
            return (from objs in table
                    where objs.patientDriversLicenseNumber == driversLicenseNum
                    select objs).FirstOrDefault();
        }
        public static List<Patient> Get(this Table<Patient> table
            , string email
            , string name
            , string driversLicenseNum
            , string prescriptionNumber
            , Int64? patientID
            , Guid? loginGuid)
        {
            email = (email + string.Empty).Trim().ToLower();
            name = (name + string.Empty).Trim().ToLower();
            driversLicenseNum = (driversLicenseNum + string.Empty).Trim().ToLower();
            prescriptionNumber = (prescriptionNumber + string.Empty).Trim().ToLower();
            if (name  != string.Empty)
            {
                return (from objs in table
                        where objs.patientFirstName.Contains(name)
                        ||
                        objs.patientLastName.Contains(name)
                        select objs).ToList();
            }
            if (driversLicenseNum != string.Empty)
            {
                return (from objs in table
                        where objs.patientDriversLicenseNumber==driversLicenseNum 
                        select objs).ToList();
            }
            if (patientID.HasValue)
            {
                return (from objs in table
                        where objs.patientID==patientID
                        select objs).ToList();
            }
            //---if email suppiled check to see if login is supplie as well
            if (prescriptionNumber != string.Empty)
            {
                return (from objs in table
                        where objs.patientPrescriptionNumber==prescriptionNumber
                        select objs).ToList();
            }
            if (email != string.Empty)
            {
                return (from objs in table
                        where objs.patientEmailAddress==email
                        select objs).ToList();
            }
            return null;

        }
    }
}
